<template>
  <div class="box">
    <van-nav-bar title="添加实名购票人" left-arrow @click-left="onClickLeft">
      <template #right>
        <van-icon name="ellipsis" @click="showMark" />
      </template>
    </van-nav-bar>
    <van-overlay :show="show">
      <div class="wrapper" @click="closeMark">
        <div class="block">
          <div @click="goHome">
            <van-icon name="wap-home-o" class="block_icon" />
            <span>主页</span>
          </div>
          <div @click="goUser">
            <van-icon name="contact" class="block_icon" />
            <span>我的聚橙</span>
          </div>
        </div>
      </div>
    </van-overlay>
    <div class="main">
      <van-form validate-first>
        <van-field v-model="realName" label="姓名" placeholder="请输入姓名" />
        <van-field v-model="realTel" type="tel" label="手机号" placeholder="请输入手机号" name="pattern" :rules="[{ pattern: pattern1, message: '请输入正确的手机号' }]" />
        <van-field v-model="realIdCart" type="digit" label="身份证号" placeholder="请输入身份证号" name="pattern" :rules="[{ pattern: pattern2, message: '请输入正确的身份证号' }]" />
        <p>*务必填写真实有效的信息,提交后不可修改</p>
      </van-form>
    </div>
    <div class="footer_btn">
      <button @click="goBack">确认提交</button>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Icon, NavBar, Overlay, Field, Form, Dialog, Toast } from 'vant'
import { realName } from '@/api'
Vue.use(NavBar)
Vue.use(Icon)
Vue.use(Overlay)
Vue.use(Form)
Vue.use(Field)
Vue.use(Dialog)
Vue.use(Toast)
export default {
  data () {
    return {
      show: false,
      realName: '',
      realTel: '',
      pattern1: /^1[3456789]\d{9}$/,
      pattern2: /(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
      realIdCart: ''
    }
  },
  mounted () {},
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    showMark () {
      this.show = true
    },
    closeMark () {
      this.show = false
    },
    goHome () {
      this.$router.push('/home')
    },
    goBack () {
      Dialog.confirm({
        message: '消息确认无误,确认提交'
      }).then(() => {
        if (this.realName !== '' && this.realTel !== '' && this.realIdCart !== '') {
          realName({
            userid: localStorage.getItem('userid'),
            token: localStorage.getItem('token'),
            IdCart: this.realIdCart
          }).then(res => {
            if (res.data.message === 'token验证失败') {
              Toast('您登录的用户信息已过期,请重新登录')
              localStorage.clear()
              setTimeout(() => {
                this.$router.replace('/login')
              }, 1000)
            } else {
              this.$router.replace('/realName')
            }
          })
        }
      }).catch(() => {
        // on cancel
      })
    },
    goUser () {
      if (localStorage.getItem('loginState') !== 'ok') {
        this.$router.push('/login')
      } else {
        this.$router.push('/user')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  display: flex;
  position: relative;
  height: 100%;
}
.block {
  width: 1.4rem;
  height: 0.9rem;
  position: absolute;
  top: 0.4rem;
  right: 0.05rem;
  border-radius: 0.05rem;
  background-color: #fff;
  div {
    width: 100%;
    height: 0.44rem;
    line-height: 0.44rem;
    .block_icon {
      font-size: 0.2rem;
      margin: 0 0.13rem;
      float: left;
      margin-top: 0.1rem;
    }
    span {
      float: left;
    }
  }
  div:nth-of-type(1) {
    border-bottom: 1px solid #f2f2f2;
  }
}
.main {
  width: 100%;
  height: auto;
  padding: 0 0.15rem;
  p {
    line-height: 0.3rem;
    height: 0.3rem;
    color: #666666;
    font-size: 0.12rem;
  }
}
.footer_btn {
  width: 100%;
  height: 0.47rem;
  margin-bottom: 0.2rem;
  padding: 0 0.15rem;
  position: fixed;
  bottom: 0;
  z-index: 100;
  button {
    background: linear-gradient(#ff6742, #ff6742),linear-gradient(#ffde00, #ffde00);
    height: 0.47rem;
    line-height: 0.47rem;
    width: 100%;
    border: none;
    color: white;
    font-size: 0.18rem;
    border-radius: 0.26rem;
  }
}
/deep/ .van-icon-arrow-left {
  font-size: 0.2rem;
  color: #232323;
}
/deep/ .van-nav-bar {
  border-bottom: 1px solid #ebebeb;
}
/deep/ .van-icon-ellipsis {
  font-size: 0.2rem;
  color: #232323;
}
/deep/ .van-field {
  width: 100%;
  height: 0.6rem;
  padding: 0;
  color: #232323;
  font-size: 0.16rem;
  line-height: 0.6rem;
  border-bottom: 1px solid #EEEEEE;
}
</style>
